package com.lcz.jms.producer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lcz.jms.bean.MsgBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.annotation.Resource;
import javax.jms.*;

/**
 * Created by luchunzhou on 2017/10/18.
 */
public class ProducerServiceImpl implements ProducerService {

    @Autowired
    JmsTemplate jmsTemplate;

    @Resource(name = "queueDestination")
    Destination destination;

    @Override
    public void sendMessage(final MsgBean msgBean) {
        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(JSONObject.toJSONString(msgBean));
            }
        });
        System.out.println("发送消息：" + JSON.toJSONString(msgBean));
    }


    @Override
    public void sendMessage2(final MsgBean msgBean) {
        //使用jmsTemplate发送消息
        jmsTemplate.send(destination, new MessageCreator() {
            //创建一个消息
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(JSON.toJSONString(msgBean));
                return textMessage;
            }
        });
        System.out.println("发送消息：" + JSON.toJSONString(msgBean));
    }
}
